﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Threading;
using System.Security.Principal;
using Locator.PersonalLocations;
using System.Globalization;
//using Locator.BusinessEntities;
using DataAccess.UnitTests;
using DataAccess.BusinessEntities;

namespace PersonalLocations.Tests
{
    [TestFixture]
    public class PersonalLocationsControllerTests : DataTestBase
    {
        [Test]
        public void GetPersonalLocationsForUser()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (PersonalLocationsController controller = new PersonalLocationsController(this.DomainContext, null))
            {
                List<PersonalLocation> locations = controller.GetPersonalLocationsForCurrentUser();
                Assert.AreEqual(2, locations.Count);
                Assert.AreEqual(Constants.PersonalLocationId, locations[0].Id);
            }
        }

        [Test]
        public void GetSingleLocationForUser()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (PersonalLocationsController controller = new PersonalLocationsController(this.DomainContext, null))
            {
                PersonalLocation location = controller.GetPersonalLocationForCurrentUser(Constants.PersonalLocationId);
                Assert.AreEqual(Constants.PersonalLocationId, location.Id);
            }
        }

        [Test]
        public void AddAndDeletePersonalLocationForUser()
        {
            Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
            using (PersonalLocationsController controller = new PersonalLocationsController(this.DomainContext, null))
            {
                // Get initial count
                var locations = controller.GetPersonalLocationsForCurrentUser();
                int nitialLocationCount = locations.Count;
                // Add one
                string newLocationDescription = "Home2";
                controller.AddPersonalLocationForCurrentUser(newLocationDescription, 44.00, -79.732728);
                // Get current list and check count is now +1
                locations = controller.GetPersonalLocationsForCurrentUser();
                Assert.AreEqual(nitialLocationCount + 1, locations.Count);
                // Find the new location's GUID
                PersonalLocation newLocation = null;
                foreach (var location in locations)
                {
                    if (location.Description == newLocationDescription)
                    {
                        newLocation = location;
                    }
                }
                // Remove the new one
                controller.DeletePersonalLocationForCurrentUser(newLocation.Id);
                // Get current list and check count is now the original value
                locations = controller.GetPersonalLocationsForCurrentUser();
                Assert.AreEqual(nitialLocationCount, locations.Count);
            }
        }
    }
}
